package com.zzy.controller;

import com.github.pagehelper.PageInfo;
import com.zzy.model.User;
import com.zzy.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping
    @ResponseBody
    public List<User> getAll() {
        List<User> users = userService.findAll();
        return users;
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    public List<User> findList(User user) {
        return userService.findList(user);
    }




    @RequestMapping(value = "/add/{name}/{address}")
    public User addUser(@PathVariable String name,
                        @PathVariable String address) {
        User user = new User();
        user.setName(name);
        user.setAddress(address);
        userService.saveUser(user);
        return user;
    }

    @RequestMapping(value = "/save")
    public Integer save( User user) {
         int i = userService.saveUser(user);
        System.out.println(user.getId());//这里能获取到 保存后 返回的 ID
        return i;
    }

    @RequestMapping(value = "/update")
    public Integer update( User user) {
        return userService.update(user);
    }


    @RequestMapping(value = "/delete/{id}")
    public Integer delete(@PathVariable String id) {
        return userService.deletes(id);
    }



    @RequestMapping(value = "/{id}")
    public User getUser(@PathVariable String id) {
        User user = userService.findOne(id);
        return user;
    }

    @RequestMapping(value = "/get")
    public User findModelByParam(User user) {
        return userService.findModelByParam(user);
    }



    @RequestMapping(value = "/search/name/{name}")
    public List<User> getByName(@PathVariable String name) {
        List<User> users = userService.findByName(name);
        return users;
    }

    @RequestMapping(value = "/page")
    public PageInfo<User> fineByPage(User user, int pageCount,int pageSize) {
        return userService.findByPage(user , pageCount,pageSize);
    }


}